package com.linjiahao.serviceChat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linjiahao.serviceChat.entity.Chatmessage;
import com.linjiahao.serviceChat.mapper.ChatmessageMapper;
import com.linjiahao.serviceChat.service.ChatmessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-18
 */
@Service
public class ChatmessageServiceImpl extends ServiceImpl<ChatmessageMapper, Chatmessage> implements ChatmessageService {

    @Override
    public boolean saveMessage(JSONObject jsonObject) {
        Chatmessage chatmessage = new Chatmessage();
        chatmessage.setData(jsonObject.getString("date"));
        chatmessage.setMyId(jsonObject.getString("myid"));
        chatmessage.setFriendId(jsonObject.getString("her_id"));
        chatmessage.setImg(jsonObject.getString("img"));
        chatmessage.setMine(jsonObject.getString("mine"));
        chatmessage.setText(jsonObject.getString("text"));
        chatmessage.setName(jsonObject.getString("name"));
        int insert = this.baseMapper.insert(chatmessage);
        Chatmessage chatmessage2 = new Chatmessage();
        chatmessage2.setData(jsonObject.getString("date"));
        chatmessage2.setMine("0");
        chatmessage2.setMyId(jsonObject.getString("her_id"));
        chatmessage2.setFriendId(jsonObject.getString("myid"));
        chatmessage2.setImg(jsonObject.getString("img"));
        chatmessage2.setText(jsonObject.getString("text"));
        chatmessage2.setName(jsonObject.getString("name"));
        int insert1 = this.baseMapper.insert(chatmessage2);
        if(insert==1 && insert1==1){
            return  true;
        }
        return false;
    }

    @Override
    public List<Chatmessage> getMessage(String her_id, String id) {
        List<Chatmessage> chatList = this.baseMapper.selectList(new QueryWrapper<Chatmessage>()
                .eq("my_id", id).eq("friend_id", her_id)
                .orderByAsc("gmt_create"));
        return chatList;
    }
}
